package com.pureut.storage.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.storage.dto.SubcontractReceiptDto;
import com.pureut.storage.export.SubcontractPurchaseReceiptExport;
import com.pureut.storage.service.SubcontractReceiptService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/***
 * 委外入库控制层
 * Author:C
 * Date:2022/12/27 16:37
 * @DESC
 */
@RestController
@RequestMapping("/subcontract-receipt")
public class SubcontractReceiptController extends BaseController {

    @Resource
    SubcontractReceiptService subcontractReceiptService;


    /**
     * 获取委外入库分页列表
     */
    @RequiresPermissions("warehouse:warehousing:subcontracting:list")
    @GetMapping("/list")
    public TableDataInfo getList(SubcontractReceiptVo subcontractReceiptVo) {
        startPage();
        //查询委外入库列表
        List<SubcontractReceiptDto> list = subcontractReceiptService.getReceiptList(subcontractReceiptVo);
        return getDataTable(list);
    }

    /**
     * 新增委外入库
     */
    @RequiresPermissions("warehouse:warehousing:subcontracting:add")
    @Log(title = "新增委外入库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SubcontractReceiptVo subcontractReceiptVo) throws Exception {
        return toAjax(subcontractReceiptService.addSubcontractReceipt(subcontractReceiptVo));
    }

    /**
     * 新增委外入库（自动新增 其他模块插入数据时使用）
     */
    @Log(title = "新增委外入库", businessType = BusinessType.INSERT)
    @PostMapping("/addOther")
    public AjaxResult addOther(@RequestBody SubcontractReceiptOtherVo subcontractReceiptOtherVo) throws Exception {
        return toAjax(subcontractReceiptService.addOtherSubcontractReceipt(subcontractReceiptOtherVo));
    }

    /**
     * 关结委外入库
     */
    @RequiresPermissions("warehouse:warehousing:subcontracting:closing")
    @Log(title = "关结委外入库", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public AjaxResult close(@RequestBody SubcontractReceiptVo subcontractReceiptVo) {
        return toAjax(subcontractReceiptService.close(subcontractReceiptVo));
    }

    /**
     * 查看
     */
    @RequiresPermissions("warehouse:warehousing:subcontracting:query")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) {
        return AjaxResult.success(subcontractReceiptService.getView(id));
    }

    /**
     * 修改委外入库
     */
    @RequiresPermissions("warehouse:warehousing:subcontracting:edit")
    @Log(title = "修改委外入库", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateSubcontractReceipt(@Validated @RequestBody SubcontractReceiptVo subcontractReceiptVo) {
        return toAjax(subcontractReceiptService.updateSubcontractReceipt(subcontractReceiptVo));
    }

    /**
     * 删除委外入库
     */
    @RequiresPermissions("warehouse:warehousing:subcontracting:remove")
    @Log(title = "删除委外入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{idStr}")
    public AjaxResult deleteSubcontractReceipt(@PathVariable("idStr") String idStr) {
        return toAjax(subcontractReceiptService.deleteSubcontractReceipt(idStr));
    }

    /**
     * 入库/送检
     */
    @RequiresPermissions("warehouse:warehousing:subcontracting:deliver")
    @Log(title = "入库/送检", businessType = BusinessType.INSERT)
    @PostMapping("/subcontractWarehousInspection")
    public AjaxResult subcontractWarehousInspection(@RequestBody SubcontractWarehousInspectionVo subcontractWarehousInspectionVo) throws Exception {
        return toAjax(subcontractReceiptService.subcontractWarehousInspection(subcontractWarehousInspectionVo));
    }

    /**
     * 导出
     */
    @RequiresPermissions("warehouse:warehousing:subcontracting:export")
    @PostMapping("/subcontractMessageExport")
    public AjaxResult subcontractMessageExport(HttpServletResponse response, SubcontractReceiptVo subcontractReceiptVo) throws IOException {
        List<SubcontractPurchaseReceiptExport> infoExport = subcontractReceiptService.getInfoExport(subcontractReceiptVo);
        ExcelUtil<SubcontractPurchaseReceiptExport> excelUtil = new ExcelUtil<SubcontractPurchaseReceiptExport>(SubcontractPurchaseReceiptExport.class);
        excelUtil.excelWriter(response, infoExport, "委外入库信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * 委外入库（pda）
     */
    @RequiresPermissions("warehouse:warehousing:subcontracting:warehousing")
    @PostMapping("/subcontractPDA")
    @Log(title = "采购入库(pda)", businessType = BusinessType.UPDATE)
    public AjaxResult subcontractPda(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return AjaxResult.success(subcontractReceiptService.subcontractPda(pickingRestDbVo));
    }

    /**
     * 委外入库状态及数据修改（委外订单关结时 fegin调用）
     */
    @PostMapping("/updateSubcontractReceiptStatus")
    public AjaxResult updateSubcontractReceiptStatus(@RequestBody UpdateReceiptStatusVo updateReceiptStatusVo) {
        return AjaxResult.success(subcontractReceiptService.updateSubcontractReceiptStatus(updateReceiptStatusVo));
    }
}
